<?php

namespace App\Modules\Admin\Entities\Member;
use App\Models\Model;

use App\Modules\Admin\Entities\Equipment\EquipmentParamsFreeModel;
use App\Modules\Admin\Entities\Equipment\EquipmentParamsModel;
use App\Modules\Admin\Entities\Equipment\EquipmentParamsPurifyModel;
use App\Modules\Admin\Entities\Equipment\EquipmentParamsSealModel;
use App\Modules\Admin\Entities\Water\ApparatusElectricityLogModel;
use App\Modules\Admin\Entities\Water\ApparatusElectricityModel;
use App\Modules\Admin\Entities\Water\ApparatusPropertyFeeLogModel;
use App\Modules\Admin\Entities\Water\ApparatusPropertyFreeModel;
use App\Modules\Admin\Entities\Water\ApparatusWaterRateModel;
use App\Modules\Admin\Entities\Water\CommunityModel;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany;

class ApparatusModel extends Model
{
    protected $table = 'apparatuses';
    protected $primaryKey='id';

    const  WATER_MANAGE = 1;//水机管理人
    const  WATER_BUY = 2;//水机购买人



    public function getApparatusByCommunityId($communityId){
       return $this->where(['community_id'=>$communityId,'is_delete'=>0])->get()->toArray();
    }

    public function getSelectListsById($ids){
        return $this->with(['community'])->whereIn('id',$ids)->orderBy($this->getKeyName(), 'ASC')
            ->get(['id','title','code','mineral_title','rich_title','community_id','address'])->toArray();
    }

    /**
     * @return HasOne
     */
    public function community(): HasOne
    {
        return $this->hasOne(CommunityModel::class, 'id', 'community_id')->select(['id','title']);
    }

    /**
     * @return HasOne
     */
    public function user(): HasOne
    {
        return $this->hasOne(UsersModel::class, 'id', 'buy_user_id')->select(['id','username','nickname','mobile']);
    }
    /**
     * 物业费 添加
     * @return HasOne
     */
    public function propertyFreeInfo(): HasOne
    {
        return $this->hasOne(ApparatusPropertyFreeModel::class, 'id', 'apparatus_id');
    }
    /**
     * 物业费 log
     * @return HasOne
     */
    public function propertyFreeLogInfo(): HasOne
    {
        return $this->hasOne(ApparatusPropertyFeeLogModel::class, 'id', 'apparatus_id');
    }
    /**
     * 电费
     * @return HasOne
     */
    public function  electricityInfo(): HasOne
    {
        return $this->hasOne(ApparatusElectricityModel::class, 'id', 'apparatus_id');
    }
    /**
     *  电费 log
     * @return HasOne
     */
    public function electricityLogInfo(): HasOne
    {
        return $this->hasOne(ApparatusElectricityLogModel::class, 'id', 'apparatus_id');
    }
    /**
     * 电费
     * @return HasOne
     */
    public function waterRateInfo(): HasOne
    {
        return $this->hasOne(ApparatusWaterRateModel::class, 'id', 'apparatus_id');
    }
    /**
     * 设备参数     *
     * @return hasMany
     */
    public function params_free(): hasMany
    {
        return $this->hasMany(EquipmentParamsFreeModel::class, 'apparatus_id', 'id');
    }
    /**
     * 设备参数     *
     * @return hasMany
     */
    public function params_purify(): hasMany
    {
        return $this->hasMany(EquipmentParamsPurifyModel::class, 'apparatus_id', 'id');
    }
    /**
     * 设备参数     *
     * @return hasMany
     */
    public function params_seal(): hasMany
    {
        return $this->hasMany(EquipmentParamsSealModel::class, 'apparatus_id', 'id');
    }

}
